﻿namespace REVISIONMAX20
{
    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Windows.Forms;

    public static class MySQLADODB
    {
        public static DataSet ExecuteGetDataSet(string pCommandText, string connectionstring)
        {
            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(connectionstring))
            {
                using (MySqlDataAdapter adapter = new MySqlDataAdapter(pCommandText, connection))
                {
                    adapter.UpdateBatchSize = 500;
                    adapter.ContinueUpdateOnError = false;
                    connection.Open();
                    adapter.Fill(dataSet);
                    connection.Close();
                }
            }
            return dataSet;
        }

        public static DataTable ExecuteGetDataTable(string pCommandText, string connectionstring)
        {
            DataTable dataTable = new DataTable();
            using (MySqlConnection connection = new MySqlConnection(connectionstring))
            {
                using (MySqlDataAdapter adapter = new MySqlDataAdapter(pCommandText, connection))
                {
                    adapter.ContinueUpdateOnError = false;
                    connection.Open();
                    adapter.Fill(dataTable);
                    connection.Close();
                }
            }
            return dataTable;
        }

        public static int ExecuteNonQuery(string pCommandText, string connectionstring)
        {
            int num = 0;
            using (MySqlConnection connection = new MySqlConnection(connectionstring))
            {
                using (MySqlDataAdapter adapter = new MySqlDataAdapter())
                {
                    adapter.UpdateBatchSize = 500;
                    adapter.ContinueUpdateOnError = false;
                    adapter.SelectCommand = new MySqlCommand(pCommandText, connection);
                    connection.Open();
                    num = adapter.SelectCommand.ExecuteNonQuery();
                    connection.Close();
                }
            }
            return num;
        }

        public static string GetConnectionString(string pDataBase)
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.AllowBatch = true;
            builder.Database = pDataBase;
            builder.UserID = "root";
            builder.Password = "root";
            builder.Server = "mikedb01";
            builder.Port = 0xcea;
            builder.PersistSecurityInfo = false;
            return builder.ConnectionString;
        }

        public static ComboBox.ObjectCollection GetDatabases(ComboBox.ObjectCollection list)
        {
            return GetDatabases(list, GetConnectionString("temp"));
        }

        public static ComboBox.ObjectCollection GetDatabases(ComboBox.ObjectCollection list, string connectionstring)
        {
            DataTable table = ExecuteGetDataTable("show databases;", connectionstring);
            list.Clear();
            DataRow[] rowArray = table.Select("(not database = 'mysql' AND not database = 'information_schema')", table.Columns[0].ColumnName + " asc");
            foreach (DataRow row in rowArray)
            {
                list.Add(row[0].ToString());
            }
            return list;
        }

        public static DataTable GetTableColumns(string pTableName, string connectionstring)
        {
            return ExecuteGetDataTable("DESCRIBE `" + pTableName + "`;", connectionstring);
        }

        public static List<string> GetTables(string connectionstring)
        {
            DataTable table = ExecuteGetDataTable("show tables;", connectionstring);
            List<string> list = new List<string>();
            DataRow[] rowArray = table.Select("", table.Columns[0].ColumnName + " asc");
            foreach (DataRow row in rowArray)
            {
                list.Add(row[0].ToString());
            }
            return list;
        }

        public static Exception TestConnection(string connectionstring)
        {
            MySqlConnection connection = null;
            try
            {
                connection = new MySqlConnection(connectionstring);
                connection.Open();
                connection.Close();
            }
            catch (Exception exception)
            {
                try
                {
                    if ((connection != null) && (connection.State == ConnectionState.Open))
                    {
                        connection.Close();
                    }
                }
                catch
                {
                }
                return exception;
            }
            return null;
        }
    }
}

